

#import "JLCFErrorTipsView.h"

@interface JLCFErrorTipsView()

@property (nonatomic, strong) UIImageView *errorImg;

@property (nonatomic, strong)UIButton *refreshButton;

@property (nonatomic, strong) UIActivityIndicatorView *activityIndicator;

@end

@implementation JLCFErrorTipsView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor whiteColor]];
        
        self.errorImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_errorImg"]];
        [self.errorImg sizeToFit];
        self.errorImg.centerX = FULL_WIDTH / 2;
        self.errorImg.top = 150;
        [self addSubview:self.errorImg];
        
        UILabel *lblErrow = [UILabel labelWithText:@"网络请求失败" color:QTZ_Gray173 align:NSTextAlignmentCenter font:FONT(12) background:[UIColor clearColor] frame:CGRectMake(0, 0, 200, 16)];
        lblErrow.centerX = self.width/2.0;
        lblErrow.top = self.errorImg.bottom + 10;
        [self addSubview:lblErrow];
        
        UILabel *lblMessage = [UILabel labelWithText:@"请检查您的网络，重新加载吧" color:QTZ_Gray173 align:NSTextAlignmentCenter font:FONT(12) background:[UIColor clearColor] frame:CGRectMake(0, 0, self.width, 16)];
        lblMessage.centerX = self.width/2.0;
        lblMessage.top = lblErrow.bottom + 10;
        [self addSubview:lblMessage];
                

        self.refreshButton = [UIButton new];
        [self.refreshButton setTitle:@"重新加载" forState:0];
        [self.refreshButton setTitleColor:QTZ_Gray73 forState:UIControlStateNormal];
        [self.refreshButton setTitleColor:QTZ_Gray173 forState:UIControlStateHighlighted];
        [self.refreshButton setBackgroundColor:QTZ_White];
        self.refreshButton.titleLabel.font = FONT(13);
        self.refreshButton.height = 44.f;
        self.refreshButton.width = 130.f;
        self.refreshButton.top = lblMessage.bottom + 20.f;
        self.refreshButton.centerX = self.width/2.0;
        self.refreshButton.layer.borderColor = QTZ_Gray242.CGColor;
        self.refreshButton.layer.borderWidth = 1;
        [self.refreshButton addTarget:self action:@selector(refresh) forControlEvents:UIControlEventTouchUpInside];
        [self.refreshButton addTarget:self action:@selector(changeTouchDownColor) forControlEvents:UIControlEventTouchDown];
        [self.refreshButton addTarget:self action:@selector(resetColor) forControlEvents:UIControlEventTouchUpInside];
        [self.refreshButton addTarget:self action:@selector(resetColor) forControlEvents:UIControlEventTouchUpOutside];
        [self addSubview:self.refreshButton];
    }
    
    return self;
}


- (void)changeTouchDownColor
{
    [self.refreshButton setBackgroundColor:QTZ_Gray250];
}

- (void)resetColor
{
    [self.refreshButton setBackgroundColor:QTZ_White];
}

- (void)beginRefresh
{
    [self refresh];
}

- (void)refresh
{
    if ([self.delegate respondsToSelector:@selector(JLCFErrorTipsViewBeginRefresh:)]) {
        [self addActivityIndicator];
        [self performSelector:@selector(callDelegate) withObject:nil afterDelay:1.5];
    }
}

- (void)callDelegate
{
    [self.delegate JLCFErrorTipsViewBeginRefresh:self];
}

- (void)addActivityIndicator
{
    [self.refreshButton setTitle:@" " forState:UIControlStateNormal];
    if (!self.activityIndicator) {
        self.activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
        self.activityIndicator.center = CGPointMake(self.refreshButton.width / 2, self.refreshButton.height / 2);
        self.activityIndicator.color = RGB(153, 153, 153);
    }
    if (!self.activityIndicator.superview) {
        [self.refreshButton addSubview:self.activityIndicator];
        [self.activityIndicator startAnimating];
    }
    self.refreshButton.enabled = NO;
}

- (void)removeActivityIndicator
{
    [self.activityIndicator stopAnimating];
    [self.activityIndicator removeFromSuperview];
    [self.refreshButton setTitle:@"重新加载" forState:UIControlStateNormal];
    self.refreshButton.enabled = YES;
}

- (void)didFinishLoading
{
    [self removeActivityIndicator];
}
@end
